﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Globalization;
using Microsoft.VisualBasic.FileIO ;
using LPV_POS.Module;
//using System.Data;



public partial class mh_save : DevExpress.XtraEditors.XtraForm
{
    cpublic c_public = new cpublic();
    public string path_excel = "";
    private string fullPath = "";
    //private BindingSource bdSource;

    public mh_save()
    {

        InitializeComponent();

    }

    public mh_save(string _sTenFile)
    {
        InitializeComponent();
        txt_ten.Text = _sTenFile;
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable("Drives");
        dt.Columns.Add("Odia");

        //==========================================
        DriveInfo[] drives = DriveInfo.GetDrives();

        foreach (DriveInfo d in drives)
        {
            //  comboBox1.Items.Add(d.Name);
            DataRow dr = dt.NewRow();
            dr.SetField("Odia", d.Name);
            dt.Rows.Add(dr);
        }

        axCombobox1.Datasource = dt;
        axCombobox1.ValueFiled = "Odia";
        axCombobox1.DisplayField = "Odia";
        axCombobox1._Init();

        SetFileColumns();
        //==========================================
        //gan menu tuy chon cach hien thi len dropdownbutton

        dropdown_view.ContextMenuBelow = contextMenuStrip1;
        
        string path = "";
        path = c_public.Registry_Get("path_pdf");
        if (!Directory.Exists(path))
            path = Application.StartupPath.Trim() + "//PDFFILE";
        if (path != "")
        {
            LoadFilesFromFolder(path, imageList1);
            axCombobox1.Text = path;
            axCombobox1.ToolTip = path;
            return;
        }
        if (Directory.Exists(Application.StartupPath + "\\PDFFILE") == true)
        {
            LoadFilesFromFolder(Application.StartupPath + "\\PDFFILE", imageList1);
            axCombobox1.Text = Application.StartupPath + "\\PDFFILE";
            axCombobox1.ToolTip = Application.StartupPath + "\\PDFFILE";
        }
        else
        {
            LoadFilesFromFolder("C:\\", imageList1);
            axCombobox1.Text = "C:\\";
            axCombobox1.ToolTip = "C:\\";
        }




    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {

        listView1.Items.Clear();


        //ImageList imL = new ImageList();
        //imL.Images.Add(Bitmap.FromFile("D:\\1.jpg"));

        //LoadFilesFromFolder(comboBox1.Text, imageList1);



    }

    private void SetFileColumns()
    {

        listView1.Items.Clear();

        ColumnHeader colHead;

        colHead = new ColumnHeader();
        colHead.Text = "Filename";
        // colHead.Width = this.listView1.ClientSize.Width - 250;
        colHead.Width = this.listView1.ClientSize.Width / 2;
        this.listView1.Columns.Add(colHead);

        colHead = new ColumnHeader();
        colHead.Text = "Size";
        colHead.Width = this.listView1.ClientSize.Width / 6;
        this.listView1.Columns.Add(colHead);

        colHead = new ColumnHeader();
        colHead.Text = "Create Time";
        colHead.Width = this.listView1.ClientSize.Width / 3;
        this.listView1.Columns.Add(colHead);

        colHead = new ColumnHeader();
        colHead.Text = "Last Modified";
        colHead.Width = this.listView1.ClientSize.Width / 3;
        this.listView1.Columns.Add(colHead);

    }

    void LoadFolders(string folder, ImageList imageList)
    {
        try
        {
            string[] lvData = new string[5];


            listView1.Items.Clear();
            // SetFileColumns();
            if (folder == "")
                return;
            DirectoryInfo dirInfo = new DirectoryInfo(folder);

            DirectoryInfo[] arrDir = dirInfo.GetDirectories();
            //===============================================


            if (arrDir != null)
            {
                foreach (DirectoryInfo dr in arrDir)
                {



                    lvData[0] = dr.Name;
                    lvData[1] = "Folder";
                    lvData[2] = dr.CreationTime.ToString();
                    lvData[3] = dr.LastWriteTime.ToString();
                    lvData[4] = dr.FullName;

                    ListViewItem lvi = new ListViewItem(lvData, 0);
                    lvi.Text = dr.Name;
                    lvi.Tag = dr.FullName;
                    lvi.Name = dr.Name;

                    this.listView1.Items.Add(lvi);


                }
            }
            //======================================================
            // gan du lieu vao listboxControl



        }
        catch (Exception ex)
        {
            ShowError(ex);
        }
    }
    //Hàm lấy tên của file
    string GetPathName(string stringPath)
    {
        string[] stringSplit = stringPath.Split('\\');
        int _maxIndex = stringSplit.Length;

        return stringSplit[_maxIndex - 1];
    }

    string FormatSize(Int64 lSize)
    {
        string stringSize = "";
        NumberFormatInfo myNfi = new NumberFormatInfo();

        Int64 lKBSize = 0;
        if (lSize < 1024)
        {
            if (lSize == 0)
            {
                stringSize = "0";
            }
            else
                stringSize = "1"; // Dung Lượng lớn hơn 0 và nhỏ hơn 1024 làm tròn thành 1 KB
        }
        else
        {
            lKBSize = lSize / 1024;
            // Chuyển sang định dạng chuẩn
            stringSize = lKBSize.ToString("n", myNfi);
            // Cắt bỏ phần thập phân.
            stringSize = stringSize.Replace(".00", "");
        }

        return stringSize + " KB";
    }

    string FormatDate(DateTime dtDate)
    {
        string stringDate = "";
        stringDate = dtDate.ToShortDateString().ToString() + " " + dtDate.ToShortTimeString().ToString();

        return stringDate;
    }
    //Thủ tục load các files trong thu muc
    void LoadFiles(string folder, ImageList imageList)
    {
        string[] lvData = new string[5];

        string[] stringFiles = Directory.GetFiles(folder);
        string stringFileName = null;

        DateTime dtCreatTime, dtModifiedTime;
        Int64 lFileSize = 0;

        try
        {


            foreach (string files in stringFiles)
            {
                stringFileName = files;
                FileInfo fi = new FileInfo(stringFileName);
                lFileSize = fi.Length;
                dtCreatTime = fi.CreationTime;
                dtModifiedTime = fi.LastWriteTime;

                lvData[0] = GetPathName(stringFileName);
                lvData[1] = FormatSize(lFileSize);
                lvData[2] = FormatDate(dtCreatTime);
                lvData[3] = FormatDate(dtModifiedTime);
                lvData[4] = fi.FullName;
                ListViewItem lvi = new ListViewItem(lvData, 0);

                switch (fi.Extension.ToUpper())
                {
                    case ".TXT":
                    case ".DAT":
                        lvi.ImageKey = "notepad.jpg";
                        break;
                    case ".DOC":
                        lvi.ImageKey = "word.gif";
                        break;
                    case ".DOCX":
                        lvi.ImageKey = "word7.jpg";
                        break;
                    case ".PDF":
                        lvi.ImageKey = "pdf.jpg";
                        break;
                    case ".JPG":
                    case ".PNG":
                        lvi.ImageKey = "jpg.jpg";
                        break;
                    case ".BMP":
                        lvi.ImageKey = "bmp.jpg";
                        break;
                    case ".CHM":
                        lvi.ImageKey = "chm.jpg";
                        break;
                    case ".COM":
                        lvi.ImageKey = "com.ico";
                        break;
                    case ".GIF":
                        lvi.ImageKey = "gif.jpg";
                        break;
                    case ".MP3":
                    case ".WMA":
                    case ".WAV":
                    case ".WMV":
                        lvi.ImageKey = "nhac.jpg";
                        break;
                    case ".XLS":
                        lvi.ImageKey = "excel3.gif";
                        break;
                    case ".XLSX":
                        lvi.ImageKey = "excel7.gif";
                        break;
                    case ".EXE":
                        lvi.ImageKey = "SETUP.ICO";
                        break;
                    case ".RAR":
                    case ".ZIP":
                        lvi.ImageKey = "nen.jpg";
                        break;
                    case ".DLL":
                        lvi.ImageKey = "dll.jpg";
                        break;

                    default:
                        lvi.ImageKey = "unkwnown.jpg";
                        break;
                }
                listView1.Items.Add(lvi);

            }

            #region code load file cu
            // FileInfo[] fileInfo = dirInfo.GetFiles();
            //foreach (FileInfo fi in fileInfo)
            //{

            //    ListViewItem lvi = new ListViewItem(lvData, 0);
            //    lvi.Text = fi.Name;
            //    lvi.Tag = fi.FullName;
            //    lvi.Name = fi.Name;
            //    lvi.ImageIndex = 13;

            //    if (fi.Extension == ".xls")
            //    {
            //        lvi.ImageIndex = 2;
            //    }
            //    if (fi.Extension == ".xlsx")
            //    {
            //        lvi.ImageIndex = 3;
            //    }
            //    if (fi.Extension == ".doc")
            //        lvi.ImageIndex = 4;
            //    if (fi.Extension == ".pdf")
            //        lvi.ImageIndex = 5;
            //    if (fi.Extension == ".mp3" || fi.Extension == ".wma")
            //        lvi.ImageIndex = 6;
            //    if (fi.Extension == ".exe")
            //        lvi.ImageIndex = 7;
            //    if (fi.Extension == ".rar" || fi.Extension == ".zip")
            //        lvi.ImageIndex = 8;
            //    if (fi.Extension == ".txt")
            //        lvi.ImageIndex = 9;
            //    if (fi.Extension == ".jpg" || fi.Extension == ".png")
            //        lvi.ImageIndex = 10;
            //    if (fi.Extension == ".gif")
            //        lvi.ImageIndex = 11;
            //    if (fi.Extension == ".dll" || fi.Extension == ".db")
            //        lvi.ImageIndex = 12;

            //    lvsi = new ListViewItem.ListViewSubItem();
            //    lvsi.Text = fi.Length.ToString();
            //    lvi.SubItems.Add(lvsi);

            //    //lvsi = new ListViewItem.ListViewSubItem();
            //    //lvsi.Text = fi.LastAccessTime.ToString();
            //    //lvi.SubItems.Add(lvsi);

            //    this.listView1.Items.Add(lvi);

            //}
            #endregion
        }
        catch (Exception ex)
        {
            ShowError(ex);
        }
    }

    void LoadFilesFromFolder(string folder, ImageList imageList)
    {

        LoadFolders(folder, imageList);

        LoadFiles(folder, imageList);


    }

    void ShowErrorPath()
    {
        MessageBox_LPV.Show("Invalid path!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }

    void ShowError(Exception ex)
    {
        MessageBox_LPV.Show(ex.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }


    private void simpleButton1_Click(object sender, EventArgs e)
    {

        if (txt_ten.Text == "" || axCombobox1.Text == "")
        {
            Mod_messagebox mess = new Mod_messagebox();
            mess.messagebox_pos("Thông báo", "Chưa nhập tên file hoặc chưa chọn địa chỉ", loaithongbao.canhbao.GetHashCode());
            return;
        }
        fullPath = axCombobox1.Text;
        //Variable.str_path = axCombobox1.Text + txt_ten.Text + ".pdf";
        if (txt_ten.Text.IndexOf(".xls") > 0)
            path_excel = axCombobox1.Text + txt_ten.Text;
        else
            path_excel = axCombobox1.Text + txt_ten.Text + ".xls";
        //    MessageBox.Show(fullPath, "Get path", MessageBoxButtons.OK, MessageBoxIcon.Information);
        Boolean ketqua = false;

        ketqua = c_public.Registry_Set("path_pdf", fullPath);



        this.Close();
    }

    private void simpleButton2_Click(object sender, EventArgs e)
    {
        //   string[] arrStr = fullPath.Split('\\');
        if (fullPath.Length != 3 && fullPath != "")
        {
            string parent = FileSystem.GetParentPath(fullPath);
            fullPath = parent;

            this.Text = parent;

            //=========================================
            axCombobox1.Text = fullPath + "\\";
            axCombobox1_SelectedIndexChanged(sender, e);

        }
        else if (fullPath.Length == 3)
        {
            axCombobox1.SelectedIndex = -1;
            axCombobox1.Text = "";
            //comboBox1.Text = "";
            listView1.Items.Clear();
        }
        else
        {
            return;
        }
    }

    private void axCombobox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (axCombobox1.SelectedIndex != -1 || axCombobox1.Text != "")
        {
            string strPath = axCombobox1.Text;
            axCombobox1.ToolTip = strPath;
            listView1.Items.Clear();

            LoadFilesFromFolder(strPath, imageList1);
        }
    }

    private void listView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            if (listView1.SelectedItems.Count == 0)
                return;

            string path = "";
            ListViewItem item = listView1.FocusedItem;
            path = item.SubItems[4].Text;



            if (item.SubItems[1].Text == "Folder")
            {
                axCombobox1.Text = path + "\\";
                fullPath = axCombobox1.Text;
            }
            FileInfo f = new FileInfo(path);
            if (!f.Exists)
            {
                DirectoryInfo dr = new DirectoryInfo(path + "\\");
                if (!dr.Exists)
                {
                    ShowErrorPath();
                    return;
                }

                axCombobox1_SelectedIndexChanged(sender, e);
                //comboBox1_SelectedIndexChanged(sender, e);
                fullPath = path;

            }

        }
        catch (Exception ex)
        {
            ShowError(ex);
            return;
        }

    }

    private void tileToolStripMenuItem_Click(object sender, EventArgs e)
    {
        listView1.View = View.Tile;
    }

    private void iconsToolStripMenuItem_Click(object sender, EventArgs e)
    {
        listView1.View = View.SmallIcon;
    }

    private void listToolStripMenuItem_Click(object sender, EventArgs e)
    {
        listView1.View = View.List;
    }

    private void detailsToolStripMenuItem_Click(object sender, EventArgs e)
    {
        listView1.View = View.Details;
    }

    private void lbel_ten_Click(object sender, EventArgs e)
    {
        txt_ten.Text = new cpublic().loadformchu_point(350, 250);
    }

    private void bton_thoat_Click(object sender, EventArgs e)
    {
        //Mod_messagebox mess = new Mod_messagebox();
        //mess.messagebox_pos("Thông báo", "Bạn muốn thoát?", loaithongbao.yesno.GetHashCode());
        //if (Variable.thongbao == true)
        //{
        Variable.str_path = "";
        this.Close();
        //}
    }

}